package com.wb.fw;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;

import cn.cmgame.billing.api.GameInterface;

import com.wb.oldwomen.R;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Director.IDirectorLifecycleListener;
import com.wiyun.engine.opengl.WYGLSurfaceView;

public class GateActivity extends Activity implements IDirectorLifecycleListener {
	static {

		System.loadLibrary("wiskia");
		System.loadLibrary("xml2");
		System.loadLibrary("wiengine");
		System.loadLibrary("wiengine_binding");
		System.loadLibrary("wisound");
	}

	protected WYGLSurfaceView mGLView;

	public static Activity sInstance;

	public static Handler shandler = new Handler();

	private boolean mStarted;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        sInstance = this;
        mGLView = new WYGLSurfaceView(this);
        setContentView(mGLView);

        // demo controls music stream
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        //
    	Director.getInstance().setScaleMode(1);
    	Director.getInstance().setMaxFrameRate(25);
    	Director.getInstance().setDisplayFPS(true);
        // add lifecycle listener
        Director.getInstance().addLifecycleListener(this);
//        Director.getInstance().setDisplayFPS(true);

        GameInterface.initializeApp(this);
        SoundManager.enable(GameInterface.isMusicEnabled());

        SoundManager.loadEffect(new int[]{
        		R.raw.apple,
        		R.raw.coin0,
        		R.raw.hit1,
        		R.raw.laugh0,
        		R.raw.win,
        		R.raw.yay0,
        });
        SoundManager.setMenuClickEffect(R.raw.hit1);

//        TimePay.startTimeThread(shandler, this, 135, new TimePay.TimeCallback() {
//
//            @Override
//            public void startPay() {
//                GameScene maingame = GameScene.getInstance();
//                if (maingame != null) {
//                    if (!maingame.mIGMLayer.isShow()) {
//                        maingame.onPuase();
//                    }
//                }
//            }
//
//            @Override
//            public void paySuccess() {
//                GameScene maingame = GameScene.getInstance();
//                if (maingame != null) {
//                    if (!maingame.mIGMLayer.isShow()) {
//                        maingame.onResume();
//                    }
//                }
//            }
//
//            @Override
//            public void payFailed() {
//                sInstance.finish();
//                System.exit(1);
//            }
//        });
    }

	@Override
    public void onPause() {
        super.onPause();

        Director.getInstance().pause();
    }

    @Override
    public void onResume() {
        super.onResume();

        Director.getInstance().resume();
    }

    @Override
    public void onDestroy() {
    	Director.getInstance().end();

        super.onDestroy();
    }

	@Override
	public void onDirectorEnded() {
	}

	@Override
	public void onDirectorPaused() {
	}

	@Override
	public void onDirectorResumed() {
	}

	@Override
	public void onDirectorScreenCaptured(String path) {
	}

	@Override
	public void onSurfaceChanged(int w, int h) {
    	/*
    	 * 把起始代码放在这里, 可以避免一些屏幕尺寸问题. 因为一开始WiEngine并不知道
    	 * SurfaceView的尺寸, 所以是假设为全屏大小. 在之后的onSurfaceChanged事件中,
    	 * 正确的大小会被设置. 但是往往在onSurfaceChanged之前, 场景就已经被创建了,
    	 * 所以如果因为某种原因不是全屏大小, 那么之前创建的场景位置就有误了.
    	 *
    	 * 由于onSurfaceChanged有可能被调用多次, 用一个标志来防止多次运行. 而且这可能
    	 * 还不够, 有些情况下onSurfaceChanged可能连续被调用两次, 第一次传入的尺寸不对,
    	 * 而第二次是对的. 这种情况下, 还需要做更多的检查, 可以通过尺寸大小和屏幕朝向来检查.
    	 * 比如游戏如果是横屏的, 则w必须要大于h才认为是可以的, 反之则必须要h大于w才可以.
    	 */
    	boolean landscape = getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    	if(!mStarted && (landscape && w >= h || !landscape && h >= w)) {
    		mStarted = true;
    		Director.getInstance().runWithScene(new SplashScene());
    	}
	}

	@Override
	public void onSurfaceCreated() {
	}

	@Override
	public void onSurfaceDestroyed() {
	}
}
